﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using CONEK911Manager.DTO;

namespace CONEK911Manager.DAO
{
    class WardDAO
    {
        readonly string _connectionString = ConfigurationManager.ConnectionStrings["Conek911ConnectionString"].ConnectionString;
        public bool AddWard(WardDTO ward)
        {
            try
            {
                using (var con = new SqlConnection(_connectionString))
                {
                    const string sql = "INSERT into [Account] (WardID, WardName, DistrictID) VALUES (@wardId, @wardName, @districtId)";
                    using (var sqlAddWard = new SqlCommand(sql))
                    {
                        sqlAddWard.Connection = con;
                        sqlAddWard.Parameters.Add(new SqlParameter("@wardId", ward.WardId));
                        sqlAddWard.Parameters.Add(new SqlParameter("@wardName", ward.WardName));
                        sqlAddWard.Parameters.Add(new SqlParameter("@districtId", ward.DistrictId));
                        con.Open();
                        var result = sqlAddWard.ExecuteNonQuery();
                        con.Close();
                        if (result == 1)
                        {
                            return true;
                        }
                        return false;
                    }
                }
            } catch (Exception e)
            {
                try
                {
                    Logger.WriteLog(e.Message + "-" + e.StackTrace);
                }
                catch
                { }
                return false;
            }
        }

        public List<WardDTO> GetAllWard()
        {
            var listWard = new List<WardDTO>();
            using (var con = new SqlConnection(_connectionString))
            {
                const string sql = "SELECT * FROM [Ward]";
                using (var cmd = new SqlCommand(sql))
                {
                    cmd.Connection = con;
                    con.Open();
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var ward = new WardDTO()
                                              {
                                                  WardId = Convert.ToInt32(reader["WardID"]),
                                                  WardName = Convert.ToString(reader["WardName"]),
                                                  DistrictId = Convert.ToInt32(reader["DistrictID"]),
                                              };
                            listWard.Add(ward);
                        }
                    }
                    con.Close();
                }
            }

            return listWard;
        }
    }
}
